package com.ksd.modules.note.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ksd.common.enums.DeletedEnums;
import com.ksd.common.web.threadlocal.SystemIdThrealLocal;
import com.ksd.commons.BaseServiceImpl;
import com.ksd.modules.note.entity.AdminNoteChapter;
import com.ksd.modules.note.mapper.AdminNoteChapterMapper;
import com.ksd.modules.note.po.AdminNoteChapterSavePO;
import com.ksd.modules.note.service.IAdminNoteChapterService;
import com.ksd.modules.note.service.IAdminNoteLessonService;
import com.ksd.modules.note.vo.AdminNoteChapterVo;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

@Service
public class AdminNoteChapterServiceImpl extends BaseServiceImpl<AdminNoteChapterMapper, AdminNoteChapter> implements IAdminNoteChapterService {

    @Autowired
    private IAdminNoteLessonService adminNoteLessonService;

    /**
     * 查询列表
     * @param noteId
     * @return
     */
    public List<AdminNoteChapterVo> findAdminNoteChapters(Long noteId){
        LambdaQueryWrapper<AdminNoteChapter> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(AdminNoteChapter::getIsDeleted, DeletedEnums.UN_DELETED.getCode());
        lambdaQueryWrapper.eq(AdminNoteChapter::getSystemId,SystemIdThrealLocal.get().getSystemId());
        lambdaQueryWrapper.eq(AdminNoteChapter::getNoteId,noteId);
        lambdaQueryWrapper.orderByAsc(AdminNoteChapter::getSorted);
        List<AdminNoteChapterVo> adminNoteChapterVos = this.tranferListBo(this.list(lambdaQueryWrapper), AdminNoteChapterVo.class);
        if(CollectionUtils.isNotEmpty(adminNoteChapterVos)){
            for (AdminNoteChapterVo adminNoteChapterVo : adminNoteChapterVos) {
                adminNoteChapterVo.setChildren(adminNoteLessonService.findAdminNoteLessons(noteId,adminNoteChapterVo.getId()));
            }
        }
        return adminNoteChapterVos;
    }

    /**
     * 保存和更新
     * @param adminNoteChapterSavePO
     * @return
     */
    @Transactional(rollbackFor=Exception.class)
    public Boolean saveUpdateAdminNoteChapter(AdminNoteChapterSavePO adminNoteChapterSavePO){
        AdminNoteChapter adminNote = tranferBo(adminNoteChapterSavePO, AdminNoteChapter.class);
        adminNote.setSystemId(SystemIdThrealLocal.get().getSystemId());
        adminNote.setSystemName(SystemIdThrealLocal.get().getSystemName());
        return  this.saveOrUpdate(adminNote);
    }

    /**
     * 根据ID获取明细
     * @param id
     * @return
     */
    public AdminNoteChapterVo getAdminNoteChapterById(Long id){
        AdminNoteChapter adminNote = this.getById(id);
        AdminNoteChapterVo adminNoteVo = tranferBo(adminNote, AdminNoteChapterVo.class);
        return adminNoteVo;
    }

    /**
     * 单个物理删除
     * @return
     */
    @Transactional(rollbackFor=Exception.class)
    public Boolean removeAdminNoteChapterById(Long id){
        boolean b = this.removeById(id);
        if(b){
            adminNoteLessonService.removeByChapterId(id);
        }
        return b;
    }

    /**
     * 修改状态
     * @param id
     * @param column
     * @param value
     * @return
     */
    @Override
    @Transactional(rollbackFor=Exception.class)
    public boolean updateStatus(Long id,String column,Integer value){
        boolean b = super.updateStatus(id,column,value);
        return b;
    }

    /**
     * 修改状态
     * @param id
     * @param column
     * @param value
     * @return
     */
    @Override
    public boolean updateStatusStr(Long id,String column,String value){
        boolean b = super.updateStatusStr(id,column,value);
        return b;
    }

    /**
     * 根据小册删除节
     * @param noteId
     * @return
     */
    @Override
    public boolean removeByNoteId(Long noteId){
        LambdaQueryWrapper<AdminNoteChapter> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(AdminNoteChapter::getNoteId,noteId);
        return this.remove(lambdaQueryWrapper);
    }

    /**
     * 根据小册删除节
     * @param noteIds
     * @return
     */
    @Override
    public boolean removeByNoteIds(List<Long> noteIds){
        LambdaQueryWrapper<AdminNoteChapter> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(AdminNoteChapter::getNoteId,noteIds);
        return this.remove(lambdaQueryWrapper);
    }

    /**
     * 复制
     * @param opid
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean copyData(Long opid){
        AdminNoteChapter data = this.getById(opid);
        if(Objects.nonNull(data)){
            data.setId(null);
            data.setStatus(0);
            data.setCreateTime(LocalDateTime.now());
            data.setUpdateTime(LocalDateTime.now());
            data.setTitle(data.getTitle()+"[副本]");
            this.save(data);
        }
        return true;
    }
}
